package com.ruoyi.web.controller.process;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.ProOrder;
import com.ruoyi.common.core.domain.entity.ProOrderDetail;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.service.ITblProOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 加工订单
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/process/order")
public class TblProOrderController extends BaseController {

    @Autowired
    private ITblProOrderService proOrderService;

    /**
     * 加工订单导入
     *
     * @param file      文件
     * @param formValue 模板类型
     * @return
     * @throws Exception
     */
    @Log(title = "加工订单", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, int formValue) throws Exception {
        String operName = getUsername();

        ExcelUtil<ProOrder> proUtil = new ExcelUtil<ProOrder>(ProOrder.class);
        ProOrder proOrder = proUtil.importExcelTo(file.getInputStream(), 4);

        ExcelUtil<ProOrderDetail> util = new ExcelUtil<ProOrderDetail>(ProOrderDetail.class);
        List<ProOrderDetail> orderList = util.importExcel(file.getInputStream(), 8);
        String message = proOrderService.importProOrder(proOrder, orderList, operName);

        return success(message);
    }

    /**
     * 获取订单详情列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProOrder proOrder) {
        startPage();
        List<ProOrder> list = proOrderService.selectProOrderList(proOrder);
        return getDataTable(list);
    }

    /**
     * 根据订单编号获取该加工订单
     */
    @GetMapping(value = "/info/{orderCode}")
    public AjaxResult getProOrderInfo(@PathVariable String orderCode) {
        ProOrder proOrder = proOrderService.selectProOrderByCode(orderCode);
        return success(proOrder);
    }

    /**
     * 获取加工订单详情下的任务
     */
    @GetMapping("/detail/list")
    public TableDataInfo detailList(ProOrderDetail proOrderDetail) {
        startPage();
        List<ProOrderDetail> list = proOrderService.selectProOrderDetailList(proOrderDetail);
        return getDataTable(list);
    }


    /**
     * 删除加工订单
     *
     * @param orderCodes
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderCodes}")
    public AjaxResult removeOrder(@PathVariable String[] orderCodes) {

        return toAjax(proOrderService.deleteOrderByCodes(orderCodes));
    }


    /**
     * 删除加工订单详情任务
     *
     * @param orderCodes
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/detail/{orderCodes}")
    public AjaxResult removeOrderDetail(@PathVariable String[] orderCodes) {
        return toAjax(proOrderService.deleteOrderDetailByIds(orderCodes));
    }

    /**
     * 新增加工订单
     *
     * @param proOrder
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult addProOrder(@Validated @RequestBody ProOrder proOrder) {
        return toAjax(proOrderService.insertProOrder(proOrder));
    }

    /**
     * 修改加工订单
     *
     * @param proOrder
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult updateProOrder(@Validated @RequestBody ProOrder proOrder) {
        return toAjax(proOrderService.updateProOrder(proOrder));
    }

    /**
     * 自动生成编号  业务单元缩写+日期+三位编号
     *
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.OTHER)
    @GetMapping("/number/{businessUnit}")
    public AjaxResult generateNumber(@PathVariable String businessUnit) {
        String number = "";
        SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");
        String data = f.format(new Date(System.currentTimeMillis()));
        number = StringUtils.getNumberPrefix(businessUnit) + data;
        List<String> taskCodeLists = proOrderService.selectAllOrderCode(number);
        if (taskCodeLists.size() > 0) {
            String orderCode = taskCodeLists.get(taskCodeLists.size() - 1);
            int sub = Integer.parseInt(orderCode.substring(orderCode.length() - 3));//截取后三位
            sub++;
            number = number + String.format("%03d", sub);
        } else {
            number = number + "001";
        }
        return success((Object) number);
    }


    /**
     * 新增加工订单任务
     *
     * @param proOrderDetail
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.INSERT)
    @PostMapping("/detail/add")
    public AjaxResult addProOrderDetail(@Validated @RequestBody ProOrderDetail proOrderDetail) {
        String operName = getUsername();
        proOrderDetail.setCreateBy(operName);
        return toAjax(proOrderService.insertProOrderDetail(proOrderDetail));
    }


    /**
     * 编辑加工订单任务
     *
     * @param proOrderDetail
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.UPDATE)
    @PutMapping("/detail/update")
    public AjaxResult updateProOrderDetail(@Validated @RequestBody ProOrderDetail proOrderDetail) {
        return toAjax(proOrderService.updateProOrderDetail(proOrderDetail));
    }

    /**
     * 删除加工订单任务
     *
     * @param orderId
     * @return
     */
    @Log(title = "加工订单管理", businessType = BusinessType.UPDATE)
    @DeleteMapping("/detail/delete/{orderId}")
    public AjaxResult deleteProOrderDetail(@PathVariable Long orderId) {
        return toAjax(proOrderService.deleteOrderDetailById(orderId));
    }


    /**
     * 拆分子任务
     */
    @Log(title = "加工订单管理", businessType = BusinessType.INSERT)
    @PutMapping("/detail/split")
    public AjaxResult splitOrderTask(@RequestBody ProOrderDetail detail) {

        return toAjax(proOrderService.splitOrderTask(detail.getOrderId(),detail.getNum()));
    }
}
